package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"go.uber.org/zap"
	"strings"
	"vpom_-system/cmd/commends_of_es"
	"vpom_-system/global"
)

const (
	MODE_ES_Ctreate = iota + 1 // 创建表
	MODE_ES_Import             // 导入数据
	Mode_ES_Export             // 导出数据
)

var esMode int8

var EsDesc = strings.Join([]string{
	"该子命令支持es的初始化，功能如下:",
	"子命令的信息帮助查询例子: vpom es m=1 (执行功能1建表)",
	"1: 创建es的表",
	"2: 导出es索引库的数据",
	"3: 导入es索引库的信息",
}, "\n")

var EsCmd = &cobra.Command{
	Use:   "es",
	Short: "es初始化命令",
	Long:  EsDesc,
	Run:   EsRunServer,
}

func EsRunServer(cmd *cobra.Command, args []string) {
	switch esMode {
	case MODE_ES_Ctreate:
		if err := commends_of_es.Elasticsearch(); err != nil {
			global.Log.Error("Generate ES file failed", zap.Error(err))
			return
		} else {
			global.Log.Info("Generate ES file successfully")
		}
	case Mode_ES_Export:
		if err := commends_of_es.ElasticsearchExport(); err != nil {
			global.Log.Error("Export ES file failed", zap.Error(err))
		} else {
			global.Log.Info("Export ES file successfully")
		}
	case MODE_ES_Import:
		if num, errs := commends_of_es.ElasticsearchImport(cmd.Name()); errs != nil {
			global.Log.Error("Failed to import ES data:", zap.Error(errs))
		} else {
			global.Log.Info(fmt.Sprintf("Successfully imported ES data, totaling %d records", num))
		}
	default:
		err := cmd.Help()
		global.Log.Error("Invalid command usage", zap.Error(err))
	}
}
func init() {
	// 设置子命令的flag
	EsCmd.Flags().Int8VarP(&esMode, "mode", "M", 0, "选择es执行功能,eg: vpom es -m=1 (执行对应功能)")
}
